package com.home.cca.network;


import com.google.gson.Gson;
import com.home.cca.model.Order;
import com.home.cca.model.Result;

import java.io.IOException;
import java.util.List;

import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.RequestBody;

public class ConfirmOrdersRequest {

    private static final String API_URL = ApiClient.getBaseUrl() + "/SupplierManager/PSOrderMgr/ConfirmPSOrder";

    /**
     * 确认订单。
     */
    public static void confirm(List<Order> orders) throws IOException {
        // 构建请求体
        FormBody.Builder formBuilder = new FormBody.Builder();
        for (Order order : orders) {
            formBuilder.add("orderIds[]", String.valueOf(order.getId()));
        }
        RequestBody body = formBuilder.build();

        // 构建请求
        Request request = new Request.Builder()
                .url(API_URL)
                .post(body)
                .addHeader("Accept", "*/*")
                .addHeader("Accept-Language", "zh-CN,zh;q=0.9")
                .addHeader("Connection", "keep-alive")
                .addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .addHeader("Cookie", ApiClient.getCookie())
                .addHeader("Origin", ApiClient.getBaseUrl())
                .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36")
                .addHeader("X-Requested-With", "XMLHttpRequest")
                .build();

        // 发送请求并处理响应
        String execute = ApiClient.execute(request);
        Result<?> res = new Gson().fromJson(execute, Result.class);
        if (!res.isSuccess()) {
            throw new IOException(res.getMessage());
        }

    }
}